﻿using ShareFrame.Business.Base_Manage;
using ShareFrame.Entity.Base_Manage;
using ShareFrame.Entity.DTO;
using ShareFrame.Util;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using NSwag.Annotations;
using System.Collections.Generic;
using System.Threading.Tasks;
using ShareFrame.IBusiness.Base_Manage;

namespace ShareFrame.Api.Controllers.Base_Manage
{
    /// <summary>
    /// 系统数据适配管理控制器
    /// </summary>
    [Route("/Base_Manage/[controller]/[action]")]
    [OpenApiTag("系统数据适配管理接口", Description = "用于系统数据适配表的相关增删改查操作")]
    [ApiExplorerSettings(GroupName = "配置管理")]
    public class Base_DataAdapterController : BaseApiController
    {
        #region DI

        public Base_DataAdapterController(IBase_DataAdapterBusiness base_DataAdapterBus, IOptions<BaseDbOption> basedbOptions)
        {
            _base_DataAdapterBus = base_DataAdapterBus;
            _basedbOptions = basedbOptions.Value;
        }

        private readonly BaseDbOption _basedbOptions;
        IBase_DataAdapterBusiness _base_DataAdapterBus { get; }

        #endregion

        #region 获取
        /// <summary>
        /// 获取实体表中全部信息
        /// </summary>
        /// <param name="input">根据传入的参数查询并分页</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<Base_DataAdapter>> GetDataList(PageInput<ConditionDTO> input)
        {
            return await _base_DataAdapterBus.GetDataListAsync(input);
        }

        /// <summary>
        /// 获取指定实体信息
        /// </summary>
        /// <param name="input">传入的指定参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Base_DataAdapter> GetTheData(IdInputDTO input)
        {
            return await _base_DataAdapterBus.GetTheDataAsync(input.id);
        }

        /// <summary>
        /// 获取算运力文件上传指定邮箱
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Base_DataAdapter> GetTheFilesEmail()
        {
            return await _base_DataAdapterBus.GetTheDataAsync(_basedbOptions, "Code", "FilesEmail");
        }

        /// <summary>
        /// 获取客服电话信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Base_DataAdapter> GetTheCSH()
        {
            return await _base_DataAdapterBus.GetTheDataAsync(_basedbOptions, "Code", "CSH");
        }



        #endregion

        #region 提交
        /// <summary>
        /// 保存提交非空验证调用对应方法
        /// </summary>
        /// <param name="data">传入待操作的实体信息</param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveData(Base_DataAdapter data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                await AddData(data);
            }
            else
            {
                await UpdateData(data);
            }
        }

        /// <summary>
        /// 基础添加方法
        /// </summary>
        /// <param name="data">待添加的实体信息</param>
        /// <returns></returns>
        [HttpPost]
        [ApiPermission("Base_DataAdapter.Add")]
        public async Task AddData(Base_DataAdapter data)
        {
            await _base_DataAdapterBus.AddDataAsync(data);
        }

        /// <summary>
        /// 基础更新方法
        /// </summary>
        /// <param name="data">待更新的实体信息</param>
        /// <returns></returns>
        [HttpPost]
        [ApiPermission("Base_DataAdapter.Edit")]
        public async Task UpdateData(Base_DataAdapter data)
        {
            await _base_DataAdapterBus.UpdateDataAsync(data);
        }

        /// <summary>
        /// 基础删除方法
        /// </summary>
        /// <param name="ids">传入指定删除条件进行单项或多项删除</param>
        /// <returns></returns>
        [HttpPost]
        [ApiPermission("Base_DataAdapter.Delete")]
        public async Task DeleteData(List<string> ids)
        {
            await _base_DataAdapterBus.DeleteDataAsync(ids);
        }

        #endregion
    }
}